package com.app.sgd.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EnvioMail {
	private String emailFrom = "";

	private String[] emailTo = {};

	private String[] emailCc = {};

	private String[] emailBcc = {};

	private String sign = "";

	private String subject = "";

	private String host = "";

	private String message = "";

	public final boolean sendTextEmail() throws AddressException {
		boolean debug = false; // change to get more information
		
		// set the host
		Properties props = new Properties();
		if (getHost() != null) {
			props.put("mail.smtp.host", getHost());
			props.put("mail.transport.protocol","smtp");
			props.put("mail.smtp.auth", "false");
			props.put("mail.smtp.port", "25");
			props.put("mail.debug", "false");
		} else {
			return false;
		}

		// create some properties and get the default Session
		MyAuthenticator sAuth = new MyAuthenticator();
		Session session = Session.getDefaultInstance(props, sAuth);
		
//		session = Session.getDefaultInstance(props,);

		//Session session = Session.getDefaultInstance(props, aAuth);
		session.setDebug(debug);

		try {
			// create a message
			Message msg = new MimeMessage(session);

			// set the from            
			if (getEmailFrom() != null) {
				InternetAddress from = new InternetAddress(getEmailFrom());
				msg.setFrom(from);
			} else {
				return false;
			}

			// set the to
			InternetAddress[] addressTo = getInternetAddreses(getEmailTo());
			if (addressTo != null) {
				msg.setRecipients(Message.RecipientType.TO, addressTo);
			} else {
				return false;
			}

			// set the cc
			InternetAddress[] addressCc = getInternetAddreses(getEmailCc());
			if (addressCc != null) {
				msg.setRecipients(Message.RecipientType.CC, addressCc);
			}

			// set the bcc
			InternetAddress[] addressBcc = getInternetAddreses(getEmailBcc());
			if (addressBcc != null) {
				msg.setRecipients(Message.RecipientType.BCC, addressBcc);
			}

			// set the subject
			msg.setSubject(getSubject());

			// send a multipart message

			// create and fill the first message part
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(message, "text/html");
//			mbp1.setContent(message, "text/plain");
			// create and fill the second message part
			MimeBodyPart mbp2 = new MimeBodyPart();
			mbp2.setContent(getSign(), "text/plain");

			// create the Multipart and its parts to it
			Multipart mp = new MimeMultipart();
			mp.addBodyPart(mbp1);
//			mp.addBodyPart(mbp2);

			// add the Multipart to the message
			msg.setContent(mp);

			Transport.send(msg);
		} catch (MessagingException mex) {
			System.out.println(mex);
			mex.printStackTrace();
		}
		return true;
	}

	public final boolean sendFileHTML(String fileHTML) throws AddressException {

		boolean debug = false; // change to get more information

		System.out.println("entro a sendFileHTML");

		// set the host
		Properties props = new Properties();
		if (getHost() != null) {
			props.put("mail.smtp.host", getHost());
			props.put("mail.smtp.auth", "true");
		} else {
			return false;
		}

		// create some properties and get the default Session
		MyAuthenticator sAuth = new MyAuthenticator();
		Session session = Session.getDefaultInstance(props, sAuth);
		session.setDebug(debug);

		try {
			// create a message
			Message msg = new MimeMessage(session);

			// set the from
			if (getEmailFrom() != null) {
				InternetAddress from = new InternetAddress(getEmailFrom());
				msg.setFrom(from);
			} else {
				return false;
			}

			// set the to
			InternetAddress[] addressTo = getInternetAddreses(getEmailTo());
			if (addressTo != null) {
				msg.setRecipients(Message.RecipientType.TO, addressTo);
			} else {
				return false;
			}

			// set the cc
			InternetAddress[] addressCc = getInternetAddreses(getEmailCc());
			if (addressCc != null) {
				msg.setRecipients(Message.RecipientType.CC, addressCc);
			}

			// set the bcc
			InternetAddress[] addressBcc = getInternetAddreses(getEmailBcc());
			if (addressBcc != null) {
				msg.setRecipients(Message.RecipientType.BCC, addressBcc);
			}

			// set the subject            
			msg.setSubject(getSubject());
			msg.setHeader("Mime-Version", "1.0");
			//msg.setHeader("Content-Transfer-Encoding", "quoted-printable"); 
			//msg.setHeader("X-Mailer", "sendhtml"); 
			//msg.setHeader("Content-Type", "text/html");         
			//msg.setDataHandler(new DataHandler(new ByteArrayDataSource(fileHTML,"text/html")));

			msg.setSentDate(new Date());

			MimeMultipart mp = new MimeMultipart();
			mp.setSubType("related");

			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(fileHTML, "text/html");

			mp.addBodyPart(mbp1);

			msg.setContent(mp);
			Transport.send(msg);
//			System.out.println("ya envie...");
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
		return true;
	}

	public final boolean sendFileEmail(String filename) throws AddressException {

		boolean debug = false; // change to get more information

		File file = new File(filename);
		if (!file.exists()) {
			System.out.println("no encontro archivo " + filename);
			return false;
		}
		DataSource ds = new FileDataSource(file);
		DataHandler dh = new DataHandler(ds);

		MimeBodyPart attachment = new MimeBodyPart();
		try {
			attachment.setDataHandler(dh);
			attachment.setFileName(filename);
		} catch (Exception e) {
			System.out.println(e);
			return false;
		}

		// set the host
		Properties props = new Properties();
		if (getHost() != null) {
			props.put("mail.smtp.host", getHost());
			props.put("mail.smtp.auth", "true");
		} else {
			return false;
		}

		// create some properties and get the default Session
		MyAuthenticator sAuth = new MyAuthenticator();
		Session session = Session.getDefaultInstance(props, sAuth);
		session.setDebug(debug);

		try {
			// create a message
			Message msg = new MimeMessage(session);

			// set the from
			if (getEmailFrom() != null) {
				InternetAddress from = new InternetAddress(getEmailFrom());
				msg.setFrom(from);
			} else {
				return false;
			}

			// set the to
			InternetAddress[] addressTo = getInternetAddreses(getEmailTo());
			if (addressTo != null) {
				msg.setRecipients(Message.RecipientType.TO, addressTo);
			} else {
				return false;
			}

			// set the cc
			InternetAddress[] addressCc = getInternetAddreses(getEmailCc());
			if (addressCc != null) {
				msg.setRecipients(Message.RecipientType.CC, addressCc);
			}

			// set the bcc
			InternetAddress[] addressBcc = getInternetAddreses(getEmailBcc());
			if (addressBcc != null) {
				msg.setRecipients(Message.RecipientType.BCC, addressBcc);
			}

			// set the subject            
			msg.setSubject(getSubject());

			// send a multipart message
			// create and fill the first message part
			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(message, "text/plain");

			// create and fill the second message part
			MimeBodyPart mbp2 = new MimeBodyPart();
			mbp2.setContent(getSign(), "text/plain");

			// create the Multipart and its parts to it
			Multipart mp = new MimeMultipart();

			mp.addBodyPart(mbp1);
			mp.addBodyPart(mbp2);
			mp.addBodyPart(attachment);

			// add the Multipart to the message
			msg.setContent(mp);

			Transport.send(msg);

			System.out.println("ya envie...");

		} catch (MessagingException mex) {
			mex.printStackTrace();
		}

		return true;
	}

	private InternetAddress[] getInternetAddreses(String[] emails)
			throws AddressException {
		int n = emails.length;
		if (n == 0) {
			return null;
		}
		InternetAddress[] iaEmails = new InternetAddress[n];
		for (int i = 0; i < n; i++) {
			iaEmails[i] = new InternetAddress(emails[i]);
		}
		return iaEmails;
	}

	public String[] getEmailBcc() {
		return emailBcc;
	}

	public void setEmailBcc(String[] emailBcc) {
		this.emailBcc = emailBcc;
	}

	public String[] getEmailCc() {
		return emailCc;
	}

	public void setEmailCc(String[] emailCc) {
		this.emailCc = emailCc;
	}

	public String getEmailFrom() {
		return emailFrom;
	}

	public void setEmailFrom(String emailFrom) {
		this.emailFrom = emailFrom;
	}

	public String[] getEmailTo() {
		return emailTo;
	}

	public void setEmailTo(String[] emailTo) {
		this.emailTo = emailTo;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

}
